package com.tpp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tpp.base.mapper.CinemaSeatMapper;
import com.tpp.base.service.AdminCinemaSeatService;
import com.tpp.comm.pojo.TpCinemaSeat;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AdminCinemaSeatServiceImpl implements AdminCinemaSeatService {

    @Resource
    private CinemaSeatMapper cinemaSeatMapper;

    @Override
    public List<TpCinemaSeat> findByCinemaId(Long cinemaId) {
        return cinemaSeatMapper.selectList(new QueryWrapper<TpCinemaSeat>()
                .lambda()
                .eq(TpCinemaSeat::getCinemaId, cinemaId)
                .orderByAsc(TpCinemaSeat::getSort));
    }

    @Override
    public Map<Integer, List<TpCinemaSeat>> findByCinemaIdGroupByX(Long cinemaId) {
        List<TpCinemaSeat> tpCinemaSeats = cinemaSeatMapper.selectList(new QueryWrapper<TpCinemaSeat>()
                .lambda()
                .eq(TpCinemaSeat::getCinemaId, cinemaId)
                .orderByAsc(TpCinemaSeat::getSort));
        Map<Integer, List<TpCinemaSeat>> collect = tpCinemaSeats.stream().collect(Collectors.groupingBy(TpCinemaSeat::getX));
        return collect;
    }

    @Override
    public void addCinemaSeats(TpCinemaSeat[] tpCinemaSeats) {
        cinemaSeatMapper.inserts(tpCinemaSeats);
    }

    @Override
    @Transactional
    public void updateCinemaSeatsByCinemaGroupByX(Map<Integer, List<TpCinemaSeat>> cinemaSeats) {
        //解析map集合获取values集合
        List<TpCinemaSeat> cinemaSeatList = new ArrayList<>();
        cinemaSeats.forEach((k,v)->{
            for (TpCinemaSeat tpCinemaSeat : v) {
                cinemaSeatList.add(tpCinemaSeat);
            }
        });
        //根据状态分组
        Map<Integer, List<TpCinemaSeat>> SeatStatus = cinemaSeatList.stream().collect(Collectors.groupingBy(TpCinemaSeat::getStatus));
        List<TpCinemaSeat> status0 = SeatStatus.get(0);
        List<TpCinemaSeat> status1 = SeatStatus.get(1);
        //状态为0的组修改无座位
        if (!status0.isEmpty()) {
            Long[] status0Ids = status0.stream().map(TpCinemaSeat::getId).collect(Collectors.toList()).toArray(Long[]::new);
            for (Long status0Id : status0Ids) {
                cinemaSeatMapper.updateById(TpCinemaSeat.builder()
                        .status(0)
                        .id(status0Id)
                        .build());
            }

        }
        //状态为1的组修改座位区域状态
        if(!status1.isEmpty()) {
            Map<Integer, List<TpCinemaSeat>> collect = status1.stream().collect(Collectors.groupingBy(TpCinemaSeat::getAreaLevel));
            collect.forEach((k, v) -> {
                Long[] status1Ids = v.stream().map(TpCinemaSeat::getId).collect(Collectors.toList()).toArray(Long[]::new);
                for (Long status1Id : status1Ids) {
                    cinemaSeatMapper.updateById(TpCinemaSeat.builder()
                            .status(1)
                            .areaLevel(k)
                            .id(status1Id)
                            .build());
                }
            });
        }
    }


}
